﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommandLine;
using CommandLine.Text;
using System.IO;
using System.Runtime.Serialization;

namespace FileListBuilder
{
    class Program
    {
        public static readonly HeadingInfo _headingInfo = new HeadingInfo("FileListBuilder.exe", "V1.1");
        public static int NumberOfFileOK = 0;
        public static int NumberOfFileNOK = 0;

        static void Main(string[] args)
        {
            Console.WriteLine("FileListBuilder.exe V1.2");
            Console.WriteLine("(c) 2010-2011 Fred Berton http://fberton.blogspot.com");

            var options = new Options();
            ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
            if (!parser.ParseArguments(args, options))
                Environment.Exit(1);

            PrintConfiguration(options);
            BuildFileList(options);

            Log(-1, String.Format("Number of processed file : {0}", NumberOfFileOK),options);
            Log(-1, String.Format("Number of file in error : {0}", NumberOfFileNOK), options);
            Console.WriteLine("Press any key to stop.");
            Console.ReadKey();
        }

        private static void BuildFileList(Options options)
        {
            Log(0,"Start files searching",options);
            string[] FileNames = Directory.GetFiles(options.StartDirectory,options.FileSelector,SearchOption.AllDirectories);
            List<Document> lstDocuments = new List<Document>();
            Log(0, "End files searching", options);

            Log(0, "Start Document process", options);
            foreach (string FileName in FileNames)
            {
                Log(1, "Document : "+FileName, options);
                lstDocuments.Add(new Document(FileName, options));
                NumberOfFileOK++;
            }
            Log(0, "End Document process", options);

            IWriter outputWriter = null;
            if (options.OutputFileFormat.ToUpper() == "XML")
                outputWriter = new XMLWriter();
            else
                outputWriter = new XLSWriter();
            outputWriter.Write(options,lstDocuments);
        }

        public static void Log(int VerboseLevel,string p, Options options)
        {
            if (VerboseLevel == -1)
            {
                Console.WriteLine(p);
            }
            else
            {
                if (options.VerboseLevel == null) return;

                if (VerboseLevel <= options.VerboseLevel.Value)
                    Console.WriteLine(p);
            }
        }

        private static void PrintConfiguration(Options options)
        {
            Console.WriteLine("Configuration :");
            Console.WriteLine("Tag separator (-t) : (" + options.TagSeparator+")");
            Console.WriteLine("Code separator (-c) : (" + options.CodeSeparator + ")");
            Console.WriteLine("File Selector (-f) : (" + options.FileSelector + ")");
            Console.WriteLine("StartDirectory (-d) : (" + options.StartDirectory + ")");
            Console.WriteLine("OutputFilename (-o) : (" + options.OutputFilename + ")");
            Console.WriteLine("OutputFileFormat (-x) : (" + options.OutputFileFormat + ")");
        }

    }
}
